<?php

namespace app\logic;

class L_table
{
    /**
     * 添加字段
     * @param string $paramObj
     */
    public function addField($paramObj = "")
    {
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $nameRule=config("app.field.name");
        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'table_name' => 'require',
                'name' => 'require',
                'type' => 'require',
                'default_value' => '',
                'length' => '',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            $db_pre=app("cmsTable")::getPre();
            $table_name=$db_pre.$dataObj->request->table_name;
            $paramsObj=paramsObj();
            $paramsObj->params=array(
                "tb_name"=>$table_name,
            );
            $checkResult=app("P_table_ddl")->getField($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $tbFieldResult = $checkResult["result"];
                $type=app("P_table_ddl")->getTbFieldType($dataObj->request->type,$dataObj->request->length);
                $collation=app("P_table_ddl")->getTbFieldCollation($dataObj->request->type,"utf8mb4_general_ci");
                $default=app("P_table_ddl")->getTbFieldDefault($dataObj->request->type,$dataObj->request->default_value);
                $field=array(
                    "Field"=>$dataObj->request->name,
                    "Type"=>$type,
                    "Collation"=>$collation,
                    "Null"=>"no",
                    "Default"=>$default,
                );
                if(!isset($tbFieldResult[$dataObj->request->name])){
                    //创建字段
                    $paramsObj=paramsObj();
                    $paramsObj->params=array(
                        "tb_name"=>$table_name,
                        "field"=>$field,
                    );
                    $checkResult=app("P_table_ddl")->addTbField($paramsObj);
                    if ($checkResult["error_no"] == 0) {

                    }else{
                        $final_arr = logCallErrorMsg($final_arr, $checkResult);
                    }
                }
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        return $final_arr;
    }
}